home *** CD-ROM | disk | FTP | other *** search
/ QuickTime - The Beta Release / QuickTime - The Beta Release.iso / Programming Stuff / vdig sample / Main.c < prev    next >
Text File  |  1991-09-05  |  12KB  |  532 lines

  1. /**************************************************
  2. *
  3. * Sample Digitizing Application
  4. *
  5. * This sample program demonstrates the basic interfaces
  6. * to a video digitizer.  Note that an application will
  7. * typically be using the sequence grabber rather than
  8. * accessing the video digitizer directly.
  9. *
  10. ***************************************************/
  11.  
  12. /**************************************************
  13. *
  14. * General Info
  15. *
  16. ***************************************************/
  17. #include <QuickDraw.h>
  18. #include "Mini Player.h"
  19. #include "Components.h"
  20. #include "video digitizer.h"
  21. #include "RO364.h" 
  22. #include "DIGIUtils.h"
  23. #include "Matrix.h"
  24.  
  25.  
  26. /* MacHeaders Included */
  27. VideoDigitizerThing                gti;
  28. Component                        gt;
  29. Boolean                            ghFlipState = FALSE; 
  30. Boolean                            gvFlipState = FALSE;
  31. extern    VideoDigitizerThing        gti;
  32. extern    Component                gt;
  33. extern Boolean                    ghFlipState, gvFlipState;
  34. /* prototypes */
  35.  
  36. void InitMacintosh(void);
  37. void SetUpMenus(void);
  38. void SetUpWindows(void);
  39. void AdjustMenus(void);
  40. void enable(MenuHandle menu, short item, Boolean ok);
  41. void HandleMenu (long mSelect);
  42. void CloseEm(WindowPtr w);
  43. void DoAboutBox(void);
  44. void DoOpen(void);
  45. Boolean IsMyWindow(WindowPtr w);
  46. void HandleMouseDown(EventRecord *theEvent);
  47. void HandleMouseUp(EventRecord *theEvent);
  48. void HandleEvent(void);
  49.  
  50. /**************************************************
  51. ***************************************************
  52. *
  53. * Initialization Routines
  54. *
  55. * All of the stuff we call to get things started
  56. *
  57. ***************************************************
  58. ***************************************************/
  59.  
  60. /**************************************************
  61. *
  62. * InitMacintosh() Initialize all the managers & memory
  63. *
  64. ***************************************************/
  65. void InitMacintosh()
  66.  
  67. {
  68.     MaxApplZone();
  69.     InitGraf(&thePort);
  70.     InitFonts();
  71.     FlushEvents(everyEvent, 0);
  72.     InitWindows();
  73.     InitMenus();
  74.     TEInit();
  75.     InitDialogs(0L);
  76.     InitCursor();
  77.  
  78. }
  79.  
  80. /**************************************************
  81. *
  82. * SetUpMenus()
  83. *
  84. * Reads in the menu resources and installs them
  85. *
  86. *
  87. ***************************************************/
  88. void SetUpMenus()
  89.  
  90. {
  91.     InsertMenu(appleMenu = GetMenu(appleID), 0);
  92.     InsertMenu(fileMenu = GetMenu(fileID), 0);
  93.     InsertMenu(editMenu = GetMenu(editID), 0);
  94.     InsertMenu(optionsMenu = GetMenu(optionsID),0);
  95.     DrawMenuBar();
  96.     AddResMenu(appleMenu, 'DRVR');
  97.     
  98. }
  99.  
  100. /**************************************************
  101. *
  102. * SetUpWindows()
  103. *
  104. *    Create the video window.
  105. *
  106. ***************************************************/
  107. void SetUpWindows()
  108.  
  109. {    
  110.     /* Create the window for the picture */    
  111.     dragRect = screenBits.bounds;
  112.     vdigWindow = GetNewVWindow(128,(Ptr)0L,(WindowPtr)-1L);    /* Make a new window */
  113.     SetPort(vdigWindow);
  114.     ShowWindow(vdigWindow);
  115.  
  116. }
  117.  
  118. /**************************************************
  119. ***************************************************
  120. *
  121. *    Routines for handling menus.
  122. *
  123. ***************************************************
  124. ***************************************************/
  125.  
  126. /**************************************************
  127. *
  128. *  AdjustMenus()
  129. *
  130. *    Enable or disable the items in the Edit menu if a DA window
  131. *    comes up or goes away. Our application doesn't do anything with
  132. *    the Edit menu.
  133. *
  134. ***************************************************/
  135. void AdjustMenus()
  136. {
  137.     register WindowPeek wp = (WindowPeek) FrontWindow();
  138.     short kind = wp ? wp->windowKind : 0;
  139.     Boolean DA = kind < 0, mainVis = ((WindowPeek) vdigWindow)->visible;
  140.     
  141.     enable(editMenu, 1, DA);
  142.     enable(editMenu, 3, DA);
  143.     enable(editMenu, 4, DA);
  144.     enable(editMenu, 5, DA);
  145.     enable(editMenu, 6, DA);
  146.     
  147.     enable(fileMenu, openItem, !((WindowPeek) vdigWindow)->visible);
  148.     enable(fileMenu, closeItem, DA || ((WindowPeek) vdigWindow)->visible);
  149.  
  150. }
  151.  
  152. static
  153. void enable(MenuHandle menu, short item, Boolean ok)
  154. {
  155.     if (ok)
  156.         EnableItem((MenuHandle)menu, item);
  157.     else
  158.         DisableItem((MenuHandle)menu, item);
  159. }
  160.  
  161. /**************************************************
  162. *
  163. *  HandleMenu (mSelect)
  164. *
  165. *    Handle the menu selection. mSelect is what MenuSelect() and
  166. *    MenuKey() return: the high word is the menu ID, the low word
  167. *    is the menu item
  168. *
  169. ***************************************************/
  170. void HandleMenu (mSelect)
  171.  
  172. long    mSelect;
  173.  
  174. {
  175.     int            menuID = HiWord(mSelect);
  176.     int            menuItem = LoWord(mSelect);
  177.     Str255        name;
  178.     GrafPtr        savePort;
  179.     WindowPeek    frontWindow;
  180.     
  181.     Ptr        bAddr;
  182.     short    rowBytes;
  183.     short    pixelSize;
  184.     short    tx;
  185.     short    ty;
  186.     short    straddle;
  187.     MatrixRecord aMatrix;
  188.     long        result = 0;
  189.     
  190.     switch (menuID)
  191.     {
  192.         case    appleID:                /* The Apple menu */
  193.             switch (menuItem)
  194.             {
  195.                 case    aboutItem:
  196.                     DoAboutBox();        /* Display the About Box */
  197.                     break;
  198.                     
  199.                 default:                /* It's a DA */
  200.                     GetPort(&savePort);
  201.                     GetItem(appleMenu, menuItem, name);
  202.                     OpenDeskAcc(name);
  203.                     SetPort(savePort);
  204.                     break;
  205.             }
  206.             break;
  207.     
  208.           case    fileID:                    /* The file menu */
  209.             switch (menuItem)
  210.               {
  211.                   case    openItem:
  212.                     break;
  213.                                 
  214.                   case    closeItem:
  215.                     if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  216.                           break;
  217.               
  218.                 if (frontWindow->windowKind < 0)
  219.                           CloseDeskAcc(frontWindow->windowKind);
  220.                     else
  221.                         /* It's the movie window ( the only one we've got ) */
  222.                         /* Close it & clean up the movie stuff */
  223.                         DisposeVideoWindow( (WindowPtr) frontWindow);
  224.  
  225.                     break;
  226.  
  227.                                                             
  228.                 case    quitItem:        /* Pack up and go home */
  229.                     /* Clean up if movie still there */
  230.                     if (((WindowPeek) vdigWindow)->visible)    
  231.                         DisposeVideoWindow(vdigWindow);    
  232.                     ExitToShell();
  233.                     break;
  234.             }
  235.             break;
  236.                   
  237.         case    editID:
  238.             if (!SystemEdit(menuItem-1))
  239.                   SysBeep(5);
  240.             break;
  241.             
  242.         case    optionsID:
  243.             switch (menuItem)
  244.             {
  245.                 case kFullSize:
  246. /*                    result = VDGetDestination(gti,&bAddr, &rowBytes, &pixelSize, &aMatrix); */
  247.                     SetIdentityMatrix(&aMatrix);
  248. /*                    result = VDSetDestination(gti,bAddr, rowBytes, pixelSize, &aMatrix); */
  249.                 break;
  250.                 
  251.                 case kQuarterSize:
  252.                 break;
  253.                 
  254.                 case kSixteenthSize:
  255.                 break;
  256.                 
  257.                 case kFreeze:
  258.                     VDSetPlayThruOnOff(gti,vdPlayThruOff);
  259.                 break;
  260.                 
  261.                 case kUnFreeze:
  262.                     VDSetPlayThruOnOff(gti,vdPlayThruOn);
  263.                 break;
  264.                 
  265.                 case kGrabOneFrame:
  266.                     VDGrabOneFrame(gti);
  267.                 break;
  268.                 
  269.                 case kFlipHoriz:
  270.                     ghFlipState = ~ghFlipState;
  271.                     UpdateVDDestination(vdigWindow);
  272.                 break;    
  273.  
  274.                 case kFlipVert:
  275.                     gvFlipState = ~gvFlipState;
  276.                     UpdateVDDestination(vdigWindow);
  277.                 break;    
  278.                 
  279.                 default:
  280.                 break;
  281.             }
  282.             break;
  283.       }
  284. }
  285.  
  286.  
  287. /**************************************************
  288. *
  289. *  DoAboutBox() Draws and displays the about box
  290. *
  291. ***************************************************/
  292. void DoAboutBox()
  293.  
  294. {
  295.     DialogPtr    theDialog;        /* Stuff for the about box */
  296.     int            itemHit, io;
  297.     Handle        tempHandle;
  298.     Rect        box;
  299.     
  300.     theDialog = GetNewDialog(aboutDlgID,0L,(WindowPtr)-1L);
  301.     
  302.     /* Highlight the default button */
  303.     SetPort(theDialog);
  304.     GetDItem(theDialog,1,&itemHit,&tempHandle,&box);
  305.     PenSize(3,3);
  306.     InsetRect(&box,-4,-4);
  307.     FrameRoundRect(&box,16,16);
  308.     
  309.     /* Wait until the button is pressed */
  310.     do
  311.         ModalDialog(0l,&itemHit);
  312.     while (itemHit != 1);
  313.     
  314.     /* Button pressed, go home */
  315.     DisposDialog(theDialog);
  316. }
  317.  
  318.  
  319. /**************************************************
  320. ***************************************************
  321. *
  322. *        The window routines
  323. *
  324. ****************************************************
  325. ***************************************************/
  326.         
  327. /**************************************************
  328. *
  329. * IsMyWindow(w):Boolean 
  330. *    Checks if w is a valid window and mine
  331. *
  332. ***************************************************/
  333. Boolean IsMyWindow(w)
  334.  
  335. WindowPtr    w;
  336.  
  337. {
  338.     return( (w != 0L) && 
  339.              (w == vdigWindow) );
  340. }
  341.     
  342. /**************************************************
  343. ***************************************************
  344. *
  345. * Event handling stuff
  346. *
  347. **************************************************** 
  348. ***************************************************/
  349.  
  350. /**************************************************
  351. *
  352. * HandleMouseDown (theEvent)
  353. *
  354. *    Take care of mouseDown events.
  355. *
  356. ***************************************************/
  357. void HandleMouseDown(theEvent)
  358.  
  359. EventRecord    *theEvent;
  360.  
  361. {
  362.     WindowPtr    theWindow;
  363.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  364.     Rect        saveRect;
  365.     Point        aPoint, bPoint;
  366.     int            ok;
  367.     EventRecord    moveEvent;
  368.     Boolean        done,equal;
  369.     Point        saveMouseLoc;
  370.     KeyMap    keyMap;
  371.     Boolean    optionKeyDown;
  372.     
  373.     switch (windowCode)
  374.     {
  375.         case inSysWindow: 
  376.             SystemClick (theEvent, theWindow);
  377.             break;
  378.         
  379.           case inMenuBar:
  380.               AdjustMenus();
  381.             HandleMenu(MenuSelect(theEvent->where));
  382.             break;
  383.         
  384.           case inDrag:
  385.               if (IsMyWindow(theWindow)) {
  386.                 GetKeys(&keyMap);
  387.                 optionKeyDown = keyMap[1] & 0x4;        /*Check for option key down */
  388.         
  389.                 if (optionKeyDown) {
  390.  
  391.                       done = FALSE;
  392.                       while (!done) {
  393.                     ok = GetNextEvent (everyEvent, &moveEvent);
  394.                     equal = EqualPt(moveEvent.where,saveMouseLoc);
  395.                     if (!equal)
  396.                           MoveVideoWindow(theWindow, moveEvent.where.h, moveEvent.where.v,TRUE);
  397.                       saveMouseLoc = moveEvent.where;
  398.                     if (ok)
  399.                           switch (moveEvent.what)
  400.                             {
  401.                             case mouseDown:
  402.                                 break;
  403.             
  404.                             case mouseUp:
  405.                                 done = TRUE;
  406.                                 break;
  407.                             
  408.                             }
  409.                     }
  410.                 }
  411.                 else    {
  412.                 DragVideoWindow(theWindow, theEvent->where, &dragRect);
  413.                 }
  414.  
  415.             }
  416.                 break;
  417.             
  418.           case inContent:
  419.               if (IsMyWindow(theWindow))
  420.                 {
  421.                     if (theWindow != FrontWindow())
  422.                       SelectWindow(theWindow);
  423.                 }
  424.               break;
  425.           
  426.         case inGoAway:
  427.               if (IsMyWindow(theWindow) && TrackGoAway(theWindow, theEvent->where))
  428.                 DisposeVideoWindow(theWindow);        /* Close the appropriate windows */
  429.             break;
  430.  
  431.         case inGrow:
  432.               if (IsMyWindow(theWindow))
  433.                   GrowVideoWindow(theWindow, theEvent->where);
  434.             break;
  435.         
  436.         break;
  437.       }
  438. }
  439.  
  440. /**************************************************
  441. *
  442. * HandleMouseUp (theEvent)
  443. *
  444. *    Take care of mouseUp events for tracking the hand.
  445. *
  446. ***************************************************/
  447. void HandleMouseUp(theEvent)
  448.  
  449. EventRecord    *theEvent;
  450.  
  451. {
  452.     WindowPtr    theWindow;
  453.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  454.     
  455. /*    if( windowCode == inContent )*/
  456. /*        MovieMouseUp(theWindow,theEvent->where);*/
  457.  
  458. /*    trackingHand = false;*/
  459. }
  460.  
  461. /**************************************************
  462. *
  463. * HandleEvent()
  464. *
  465. *        The main event dispatcher. This routine should be called
  466. *        repeatedly (it  handles only one event).
  467. *
  468. ***************************************************/
  469. void HandleEvent()
  470.  
  471. {
  472.     int            ok;
  473.     EventRecord    theEvent;
  474.     WindowPtr    theWindow;
  475.     OSErr        retstat;
  476.  
  477.     HiliteMenu(0);
  478.     SystemTask ();        /* Handle desk accessories */
  479.     
  480.     ok = GetNextEvent (everyEvent, &theEvent);
  481.     if (ok)
  482.       switch (theEvent.what)
  483.         {
  484.         case mouseDown:
  485.             HandleMouseDown(&theEvent);
  486.             break;
  487.             
  488.         case mouseUp:
  489.             HandleMouseUp(&theEvent);
  490.             break;
  491.  
  492.         case keyDown: 
  493.         case autoKey:
  494.             if ((theEvent.modifiers & cmdKey) != 0)
  495.             {
  496.               AdjustMenus();
  497.               HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  498.             }
  499.             break;
  500.             
  501.         case updateEvt:
  502.             theWindow = (WindowPtr) theEvent.message;    /* Get the window */
  503.             BeginUpdate(theWindow);            
  504.             EndUpdate(theWindow);
  505.             break;
  506.             
  507.         case activateEvt:
  508.             /* Force an update event */
  509.             SetPort( (WindowPtr) theEvent.message );
  510.             InvalRect(&((WindowPtr)theEvent.message)->portRect);
  511.             break;
  512.         }
  513. }
  514.  
  515. /**************************************************
  516. *
  517. * main()
  518. *
  519. *    This is where everything happens
  520. *
  521. ***************************************************/
  522. main()
  523.  
  524. {    
  525.     InitMacintosh();        /* Initialize everything */
  526.     SetUpMenus();
  527.     SetUpWindows();
  528.  
  529.     for (;;)
  530.         HandleEvent();
  531. }
  532.